const app = getApp();
const WxNotificationCenter = require('../../../../common/utils/WxNotificationCenter/WxNotificationCenter');
WxNotificationCenter.postNotificationName(app.INJECT_BLOBAL_EVENT, {
  global: require('../../../global'),
});
Page({
  /**
   * 页面的初始数据
   */
  data: {
    roomID: '', // 房间id
    roomname: '', // 房间名称
    beauty: 5,
    muted: true,
    debug: false,
    frontCamera: true,
    createType:'',
  },

  onRoomEvent: function (e) {
    switch (e.detail.tag) {
      case 'recvTextMsg': {
        console.log('收到消息:', e.detail.detail);
        break;
      }
      case 'roomClosed': {
        /*
            房间关闭时会收到此通知，客户可以提示用户房间已经关闭，做清理操作
          */
        // 在房间内部才显示提示
        console.log('roomClose:', e.detail.detail);
        let pages = getCurrentPages();
        if (pages.length > 1 && pages[pages.length - 1].__route__ === 'cm_trtc_2/pages/multiroom/room/room') {
          wx.showModal({
            title: '提示',
            content: e.detail.detail || '房间已解散',
            showCancel: false,
            complete: function () {
              pages = getCurrentPages();
              // 点击 modal 后，判断页面是否已跳转，防止跳转两次
              if (pages.length > 1 && pages[pages.length - 1].__route__ === 'cm_trtc_2/pages/multiroom/room/room') {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
        }
        break;
      }
      case 'error': {
        // 在房间内部才显示提示
        console.error('error:', e.detail.detail);
        let pages = getCurrentPages();
        if (pages.length > 1 && pages[pages.length - 1].__route__ === 'cm_trtc_2/pages/multiroom/room/room') {
          wx.showModal({
            title: '提示',
            content: e.detail.detail,
            showCancel: false,
            complete: function () {
              pages = getCurrentPages();
              // 点击 modal 后，判断页面是否已跳转，防止跳转两次
              if (pages.length > 1 && pages[pages.length - 1].__route__ === 'cm_trtc_2/pages/multiroom/room/room') {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
        }
        break;
      }
    }
  },

  changeCamera: function () {
    let rtcroomCom = this.selectComponent('#rtcroom');
    if (rtcroomCom) {
      rtcroomCom.switchCamera();
    }
    this.setData({
      frontCamera: !this.data.frontCamera,
    });
  },
  setBeauty: function () {
    this.data.beauty = this.data.beauty == 0 ? 5 : 0;
    this.setData({
      beauty: this.data.beauty,
    });
  },
  changeMute: function () {
    this.data.muted = !this.data.muted;
    this.setData({
      muted: this.data.muted,
    });
  },
  checkDebug() {
    app.util.request({
      url: 'entry/wxapp/index',
      data: {
        op: 'checkSwitch',
      },
      module: 'cm_trtc',
      success: res => {
        if (res.data && res.data.errno == 0) {
          const data = res.data.data;
          if (data) {
            this.setData({ debug: data.is_debug == 1 });
          }
        }
      },
    });
  },
  showLog: function () {
    this.data.debug = !this.data.debug;
    this.setData({
      debug: this.data.debug,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('room.js onLoad');
    console.log(options);
    let time = new Date();
    time = time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
    console.log('*************开始多人音视频：' + time + '**************');
    this.data.role = options.type;
    this.data.roomID = options.roomID || '';
    this.data.roomname = options.roomName;
    this.data.username = options.userName;
    if (options.roomID) {
      let pages = getCurrentPages();
      let route = pages[pages.length - 1].route;
      app.videoVisiterRecord(options.roomID, route.split('/')[0]);// 音视频参与人员记录接口,参数是roomId
    }
    this.setData(
      {
      roomID: this.data.roomID,
      roomname: this.data.roomname,
      username: this.data.username,
      createType: options.type||'',
      },
      function() {
      let rtcroomCom = this.selectComponent('#rtcroom');
      if (rtcroomCom) {
        rtcroomCom.start();
        wx.showToast({
          title: '当前为静音状态，如需发言请打开声音',
          icon: 'none',
          duration: 3000,
        });
      }
      },
    );
    this.checkDebug();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 设置房间标题
    wx.setNavigationBarTitle({
      title: this.data.roomname,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    console.log('room.js onShow');
    // 保持屏幕常亮
    wx.setKeepScreenOn({
      keepScreenOn: true,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('room.js onHide');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('room.js onUnload');
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},
});
